package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysPost;
import com.morningstar.system.dao.mapper.SysPostMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 岗位信息 服务层处理
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SysPostService extends ServiceImpl<SysPostMapper, SysPost> {

    /**
     * 添加或更新职位 前校验
     *
     * @param sysPost 系统用户
     * @return 校验结果
     */
    public ServiceCheckResult checkAddAndUpdate(SysPost sysPost) {
        LambdaQueryWrapper<SysPost> checkPostNameWrapper = new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostName, sysPost.getPostName())
                .ne(sysPost.getPostId() != null, SysPost::getPostId, sysPost.getPostId());
        if (this.count(checkPostNameWrapper) > 0) {
            return new ServiceCheckResult(true, "职位名称已存在");
        }
        LambdaQueryWrapper<SysPost> checkPostCodeWrapper = new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostCode, sysPost.getPostCode())
                .ne(sysPost.getPostId() != null, SysPost::getPostId, sysPost.getPostId());
        if (this.count(checkPostCodeWrapper) > 0) {
            return new ServiceCheckResult(true, "职位编码已存在");
        }
        return new ServiceCheckResult(false);
    }
}
